package com.googlecode.microanalyst.designer.model.commands;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.gef.commands.Command;

import com.googlecode.microanalyst.designer.i18n.DesignerMessages;
import com.googlecode.microanalyst.designer.model.AnalysisElement;
import com.googlecode.microanalyst.designer.model.AnalysisGuide;
import com.googlecode.microanalyst.designer.model.AnalysisRuler;

public class DeleteGuideCommand extends Command {

	private AnalysisRuler parent;
	private AnalysisGuide guide;
	private Map<AnalysisElement, Integer> oldParts;

	public DeleteGuideCommand(AnalysisGuide guide, AnalysisRuler parent) {
		super(DesignerMessages.DeleteGuideCommand_Label);
		this.guide = guide;
		this.parent = parent;
	}

	public boolean canUndo() {
		return true;
	}

	public void execute() {
		oldParts = new HashMap<AnalysisElement, Integer>(guide.getMap());
		Iterator<AnalysisElement> iter = oldParts.keySet().iterator();
		while (iter.hasNext()) {
			guide.detachPart((AnalysisElement) iter.next());
		}
		parent.removeGuide(guide);
	}

	public void undo() {
		parent.addGuide(guide);
		Iterator<AnalysisElement> iter = oldParts.keySet().iterator();
		while (iter.hasNext()) {
			AnalysisElement part = (AnalysisElement) iter.next();
			guide.attachPart(part, ((Integer) oldParts.get(part)).intValue());
		}
	}
}
